package com.tl.khadmin.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource; 

import org.springframework.stereotype.Service;

import com.tl.khadmin.bean.Logistics;
import com.tl.khadmin.bean.ProductBrand;
import com.tl.khadmin.mapper.LogisticsMapper;
import com.tl.khadmin.mapper.ProductBrandMapper; 
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.LogisticsIService;
import com.tl.khadmin.service.ProductBrandIService;
import com.tl.khadmin.util.CommonUtil; 

@Service
public class LogisticsServiceImpl implements LogisticsIService{
	
	@Resource
	private LogisticsMapper logisticsMapper;
	
	public List<Logistics> listLogistics(Map<String, Object> param){
		return logisticsMapper.listLogistics(param); 
	}
	
	@Override
	public String updateLogistics(Logistics logistics) {
		Integer id = logisticsMapper.queryIdByNameAndId(logistics.getId(), logistics.getLogisticsName());
		if (id != null) {
			return CommonUtil.returnMapResultToJson("fail","名称已经存在");
		}  
		int result = logisticsMapper.updateByPrimaryKeySelective(logistics);
		return CommonUtil.returnMapResultByRows(result, "修改成功", "修改失败，请重试");
	}
	
	@Override
	public Page<Logistics> showLogisticsByPage(Integer pageNum, Integer pageSize, Map<String, Object> params) {
		Page<Logistics> page=new Page<Logistics>(pageNum, pageSize);
        params.put("page", page);   
        List<Logistics> list= logisticsMapper.showLogisticsByPage(params);  
		page.setResult(list);
		return page;
	}

	@Override
	public String saveLogistics(Logistics logistics) {
		Integer id = logisticsMapper.queryIdByName(logistics.getLogisticsName());
		if (id != null) {
			return CommonUtil.returnMapResultToJson("fail","=名称已经存在");
		} 
		logistics.setCreateTime(new Date()); 
		int result = logisticsMapper.insertSelective(logistics);
		return CommonUtil.returnMapResultByRows(result, "添加成功", "添加失败，请重试");
	} 
	
	@Override
	public String deleteByIds(String ids) {
		String[] idsArr = ids.substring(0,ids.length()-1).split(","); 
		try{
			if(logisticsMapper.deleteByIds(idsArr) > 0) {
				return CommonUtil.returnMapResultByRows(1, "删除成功","删除失败，请重试");
			} 
		}catch(Exception ex) {
			return CommonUtil.returnMapResultByRows(0, "删除成功","数据不能删除");
		} 
		return CommonUtil.returnMapResultByRows(0, "删除成功","删除失败，请重试");
	}

	@Override
	public Map<String, Object> queryLogisticsById(Integer id) {
		Map<String, Object> result = new HashMap<>();
		Logistics logistics = logisticsMapper.selectByPrimaryKey(id); 
		result.put("logistics", logistics); 
		return result;
	}

	@Override
	public String updateStatus(Logistics logistics) {
		int result = logisticsMapper.updateByPrimaryKeySelective(logistics);
		return CommonUtil.returnMapResultByRows(result, "修改成功", "修改失败，请重试");
	} 
}
